package com.coocaa.internship.service.serviceImpl;

import com.coocaa.internship.common.ResultData;
import com.coocaa.internship.config.FileServerConfig;
import com.coocaa.internship.dao.CompanyDao;
import com.coocaa.internship.dao.UserCvDao;
import com.coocaa.internship.dao.UserInfoDao;
import com.coocaa.internship.pojo.Company;
import com.coocaa.internship.pojo.UserCV;
import com.coocaa.internship.pojo.UserInfo;
import com.coocaa.internship.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @author :hhd
 * @Description:
 * @date :2019/8/10 3:13
 */

@Service
public class SendMailServiceImpl implements SendMailService {

    @Autowired
    JavaMailSender javaMailSender;

    @Autowired
    UserCvDao userCvDao;

    @Autowired
    UserInfoDao userInfoDao;

    @Autowired
    CompanyDao companyDao;

    @Autowired
    FileServerConfig fileServerConfig;

    @Override
    public ResultData sendSimpleMail(String subject, String text) {

        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom("562334871@qq.com");
        simpleMailMessage.setTo("hhd562334871@163.com");
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(text);
        javaMailSender.send(simpleMailMessage);
        return ResultData.success("success");
    }

    @Override
    public ResultData sendAttachmentsMail(Long userId, Long userCvId, Long companyId) throws MessagingException {

        UserCV userCV = userCvDao.selectUserCvByUserIdAndUserCvId(userId, userCvId);
        UserInfo userInfo = userInfoDao.selectUserInfoById(userId);
        Company company = companyDao.selectCompanyById(companyId);

        String subject = "hello: " + company.getCompanyName();
        String text = "i'am " + userInfo.getUserInfoName();
//        Map<String, String> attachmentMap = null;

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        //是否发送的文件是富文本（附件、图片、html等）
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);

        mimeMessageHelper.setFrom("hhd562334871@163.com");
        mimeMessageHelper.setTo(company.getCompanyEmail());
        mimeMessageHelper.setSubject(subject);
        mimeMessageHelper.setText(text, true); //重点，默认为false，显示原始的html代码，无效果

        if (userCV.getUserCvPdf() != "") {
            int s_length = fileServerConfig.getDownloadPath().length();
            String pdf_new = fileServerConfig.getUploadPath() + userCV.getUserCvPdf().substring(s_length);

            File file = new File(pdf_new);
            if (file.exists()) {
                FileSystemResource fileSystemResource = new FileSystemResource(file);
                mimeMessageHelper.addAttachment(file.getName(), fileSystemResource);
            }
        }

        javaMailSender.send(mimeMessage);
        return ResultData.success("发送成功");
    }
}
